package com.sg.business.visualization.view;

import java.util.List;

import org.bson.types.ObjectId;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.widgets.Shell;

import com.mobnut.commons.util.Utils;
import com.mobnut.db.DBActivator;
import com.mobnut.db.model.IContext;
import com.mobnut.db.model.ModelService;
import com.mobnut.db.model.PrimaryObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.sg.business.model.IModelConstants;
import com.sg.business.model.OwnerProjectSetFolder;
import com.sg.business.model.UserProjectPerf;
import com.sg.business.resource.nls.Messages;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.commons.dnd.DropPrimaryObjectTarget;
import com.sg.widgets.part.ComboInputDialog;
import com.sg.widgets.part.CurrentAccountContext;
import com.sg.widgets.viewer.ViewerControl;

public class DropProjectToOwnerSet extends DropPrimaryObjectTarget {

	private static final String TITLE = Messages.get().ProjectSetAdd_0;

	@Override
	protected void doDrop(String sourceId, List<PrimaryObject> dragsItems,
			DropTargetEvent event, ViewerControl targetViewerControl) {
		if (dragsItems == null || dragsItems.isEmpty()) {
			return;
		}
		if (event.item == null) {
			return;
		}
		Object object = event.item.getData();
		if (object instanceof UserProjectPerf) {
			UserProjectPerf userProjectPerf = (UserProjectPerf) object;
			addProjectSet(userProjectPerf, dragsItems);
			targetViewerControl.getViewer().refresh();
			// UIFrameworkUtils.refreshHomePart(true);
		} else if (object instanceof OwnerProjectSetFolder) {
			Shell shell = targetViewerControl.getControl().getShell();
			String name = getProjectSetName(shell);
			if (!Utils.isNullOrEmpty(name)) {
				UserProjectPerf userProjectPerf = ModelService
						.createModelObject(UserProjectPerf.class);
				try {
					if (dragsItems.size() > 0) {
						ObjectId[] projectIds = new ObjectId[dragsItems.size()];
						int i = 0;
						for (PrimaryObject primaryObject : dragsItems) {
							projectIds[i++] = primaryObject.get_id();
						}
						userProjectPerf.setValue(UserProjectPerf.F_PROJECT_ID,
								projectIds);
						IContext context = new CurrentAccountContext();
						userProjectPerf.setValue(UserProjectPerf.F_USERID,
								context.getAccountInfo().getConsignerId());
						userProjectPerf.setValue(UserProjectPerf.F_DESC, name);
						userProjectPerf.doSave(context);
						targetViewerControl.getViewer().refresh(object);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} else {
			MessageUtil.showToast(Messages.get().DropProjectToOwnerSet_0,
					SWT.ICON_WARNING);
		}

		super.doDrop(sourceId, dragsItems, event, targetViewerControl);
	}

	private String getProjectSetName(Shell parentShell) {
		IInputValidator val = new IInputValidator() {

			@Override
			public String isValid(String newText) {
				return Utils.isNullOrEmpty(newText) ? Messages.get().ProjectSetAdd_5
						: null;
			}
		};
		InputDialog input = new InputDialog(parentShell, TITLE,
				Messages.get().ProjectSetAdd_6, "", val);

		int ok = input.open();
		if (ok == ComboInputDialog.OK) {
			return input.getValue();
		} else {
			return null;
		}
	}

	private void addProjectSet(UserProjectPerf userProjectPerf,
			List<PrimaryObject> dragsItems) {
		List<ObjectId> userProjectId = userProjectPerf.getAllProjectId();

		for (PrimaryObject po : dragsItems) {
			ObjectId id = po.get_id();
			if (userProjectId.contains(id)) {
				userProjectId.remove(id);
			} else {
				userProjectId.add(id);
			}
		}

		DBCollection col = DBActivator.getCollection(IModelConstants.DB,
				IModelConstants.C_USERPROJECTPERF);
		DBObject update = new BasicDBObject();
		update.put("$set", new BasicDBObject().append(
				UserProjectPerf.F_PROJECT_ID,
				userProjectId.toArray(new ObjectId[0])));
		col.update(new BasicDBObject().append(UserProjectPerf.F__ID,
				userProjectPerf.get_id()), update, true, false);
	}

}
